package com.itjason.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itjason.dto.CommentDTO;
import com.itjason.dto.DeleteDynamicDTO;
import com.itjason.dto.DynamicDTO;
import com.itjason.exceptionhandler.ResponseException;
import com.itjason.po.Comments;
import com.itjason.po.Dynamics;
import com.itjason.service.CommentsService;
import com.itjason.service.DynamicsService;
import com.itjason.mapper.DynamicsMapper;
import com.itjason.vo.DynamicVO;
import com.itjason.vo.DynamicsVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author dengxiang
* @description 针对表【dynamics】的数据库操作Service实现
* @createDate 2024-06-03 21:03:04
*/
@Service
public class DynamicsServiceImpl extends ServiceImpl<DynamicsMapper, Dynamics>
    implements DynamicsService{

    @Resource
    private DynamicsMapper dynamicsMapper;

    @Resource
    private CommentsService commentsService;

    @Override
    public ResponseException addDynamic(DynamicDTO dynamicDTO) {

        Dynamics dynamics = new Dynamics();
        dynamics.setContent(dynamicDTO.getContent());
        dynamics.setImageUrl(dynamicDTO.getImgUrl());
        dynamics.setUserId(dynamicDTO.getUserId());
        dynamics.setCreatedAt(new Date());

        int insert = dynamicsMapper.insert(dynamics);

        return insert>0?ResponseException.success():ResponseException.error("200","动态发布失败");
    }

    @Override
    public ResponseException addComment(CommentDTO commentDTO) {
        Comments comments = new Comments();
        comments.setContent(commentDTO.getContent());
        comments.setUserId(commentDTO.getUserId());
        comments.setDynamicId(commentDTO.getDynamicId());
        comments.setCreatedAt(new Date());
        int result = commentsService.addComment(comments);
        return result>0?ResponseException.success():ResponseException.error("600","评论失败");
    }

    @Override
    public ResponseException deleteDynamic(DeleteDynamicDTO dynamicDTO) {

        int commentResult=commentsService.deleteComment(dynamicDTO.getUserId(),dynamicDTO.getDynamicId(),dynamicDTO.getCommentId());

        int dynamicResult=dynamicsMapper.deleteDynamic(dynamicDTO.getUserId(),dynamicDTO.getDynamicId());

        return null;
    }

    @Override
    public DynamicsVO getDynamics() {
        LambdaQueryWrapper<Dynamics> dynamicsLambdaQueryWrapper=new LambdaQueryWrapper<>();
        dynamicsLambdaQueryWrapper.eq(Dynamics::getIsDelete,0);
        List<Dynamics> dynamics = dynamicsMapper.selectList(dynamicsLambdaQueryWrapper);
        DynamicsVO dynamicsVO = new DynamicsVO();
        dynamicsVO.setDynamics(dynamics);
        return dynamicsVO;
    }

    @Override
    public List<Comments> getDynamicsAboutComments(Integer dynamicId) {
        return commentsService.selectCommentsByDynamicId(dynamicId);
    }

    @Override
    public List<DynamicVO> getDynamicByUserId(Integer userId) {
        return null;
    }
}




